package hu.myai.triggers.bullish;

import hu.myai.api.tick.AbstractTickProvider;
import hu.myai.api.triggers.AbstractTrigger;
import hu.myai.indicators.tick.TickPositionChangeIndicator;
import hu.myai.indicators.tick.TickSizeChangeIndicator;
import hu.myai.indicators.tick.TickVolumeChangeIndicator;
import hu.myai.model.IndicatorVO;
import hu.myai.model.TriggerVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Checking the last 3 tick trigger check an optimistic feeling on the market
 * TODO test with real data
 * 
 * @author bozbalint
 * 
 * */

public class AgressiveBuyTrigger extends AbstractTrigger {

	Double firstPositionValue;

	Double secondPositionValue;

	Double firstSizeValue;

	Double secondSizeValue;

	Double firstVolumeValue;

	Double secondVolumeValue;

	/**
	 * The object used when logging debug,errors,warnings and info.
	 * */
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	private String positionID;

	private String volumeID;

	private String sizeID;
	public AgressiveBuyTrigger(String name, AbstractTickProvider tick) {
		super(name, tick);
		positionID = register(new TickPositionChangeIndicator());
		volumeID = register(new TickVolumeChangeIndicator());
		sizeID = register(new TickSizeChangeIndicator());
	}


	@Override
	protected TriggerVO triggerCalculation() {

		IndicatorVO thirdPositionIndicator = indicatorMap.get(positionID);
		IndicatorVO thirdSizeIndicator = indicatorMap.get(sizeID);
		IndicatorVO thirdVolumeIndicator = indicatorMap.get(volumeID);

		// ignore calculation if no indicator value
		if (thirdPositionIndicator == null || thirdSizeIndicator == null || thirdVolumeIndicator == null) {
			return null;
		}

		// Skip calculation while this class history is not set properly
		if (firstPositionValue == null || firstSizeValue == null || secondPositionValue == null
				|| secondSizeValue == null || firstVolumeValue == null || secondVolumeValue == null) {

			firstPositionValue = secondPositionValue;
			secondPositionValue = thirdPositionIndicator.getValue();

			firstSizeValue = secondSizeValue;
			secondSizeValue = thirdSizeIndicator.getValue();

			firstVolumeValue = secondVolumeValue;
			secondVolumeValue = thirdVolumeIndicator.getValue();

			return null;
		}

		// the logic: side way trend
		if (isInPct(firstPositionValue, 5) && isInPct(firstSizeValue, 5) && isInPct(firstVolumeValue, 13)) {

			// the logic: lower tick, higher volume
			if (secondPositionValue < 1 && isInPct(secondSizeValue, 5) && secondVolumeValue > 1.3) {

				// the logic: higher prize that has volume
				if (thirdVolumeIndicator.getValue() > 0.5 && thirdPositionIndicator.getValue() > 1.02) {

					TriggerVO trigger = new TriggerVO();
					trigger.setTick(lastTick);
					trigger.setRisk(20);
					trigger.setTradeType(TriggerVO.BUY_ONMARKET);

					log.debug(trigger.toString());
					return trigger;
				}
			}
		}
		log.debug(new TriggerVO().toString());
		return null;
	}

	private boolean isInPct(double value, int limit) {
		return value < (1.0 + (limit / 100.0)) && value > (1.0 - (limit / 100.0));
	}
}
